// 导入Manager组件
import Manager, { TableColumnConfig } from "../../component/Manager/index.tsx";
import { useConfig } from "../../hook/D8dConfigProvider.tsx";
import dayjs from "dayjs";
import AdminImage from "../../component/AdminImage/index.tsx";
import MarkParse from "../../component/MarkPreview/index.tsx";
import type { DetailFieldConfig, FormItemConfigs } from "../../global.ts";

export default function ArticleManagerPage() {
  const { sysConfig } = useConfig();
  const apiConfig = {
    search: "/v1/admin/article",
    edit: "/v1/admin/article",
    add: "/v1/admin/article",
    delete: "/v1/admin/article",
  };

  const tableColumnConfig: TableColumnConfig[] = [
    {
      title: "序号",
      dataIndex: "_index",
      key: "_index",
      fixed: "left",
      width: 80,
    },
    {
      title: "封面图",
      dataIndex: "cover_url",
      key: "cover_url",
      width: 150,
      render: (text) => {
        return <AdminImage src={text} height={30} />;
      },
    },
    {
      title: "标题",
      dataIndex: "title",
      key: "title",
      width: 300,
      ellipsis: true,
    },
    {
      title: "分类",
      dataIndex: "category",
      key: "category",
    },
    {
      title: "标签",
      dataIndex: "tags",
      key: "tags",
      ellipsis: true,
    },
    {
      title: "作者",
      dataIndex: "author",
      key: "author",
    },
    // {
    //   title: "审核状态",
    //   dataIndex: "audit_status",
    //   key: "audit_status",
    //   render: (text: number) => {
    //     switch(text) {
    //       case 0: return "待审核";
    //       case 1: return "已通过";
    //       case 2: return "已拒绝";
    //       default: return "未知";
    //     }
    //   },
    // },
    {
      title: "状态",
      dataIndex: "is_deleted",
      key: "is_deleted",
      render: (text: number) => text === 1 ? "已删除" : "正常",
    },
    {
      title: "更新时间",
      dataIndex: "updated_at",
      key: "updated_at",
      render: (text: string) => dayjs(text).format("YYYY-MM-DD HH:mm:ss"),
    },
    {
      title: "操作",
      dataIndex: "operate",
      key: "operate",
    },
  ];

  const searchFormFieldConfig: FormItemConfigs[] = [
    {
      label: "关键字",
      name: "keyword",
      type: "input",
      placeholder: "标题/作者/分类/标签",
    },
    // {
    //   label: "审核状态",
    //   name: "audit_status",
    //   type: "select",
    //   options: [
    //     { label: "待审核", value: 0 },
    //     { label: "已通过", value: 1 },
    //     { label: "已拒绝", value: 2 },
    //   ],
    // },
  ];

  const addFormFieldConfig: FormItemConfigs[] = [
    {
      label: "标题",
      name: "title",
      type: "input",
      rules: [{ required: true, message: "请输入标题" }],
    },
    {
      label: "分类",
      name: "category",
      type: "input",
    },
    {
      label: "标签",
      name: "tags",
      type: "input",
      placeholder: "多个标签用逗号分隔",
    },
    {
      label: "作者",
      name: "author",
      type: "input",
    },
    {
      label: "封面图",
      name: "cover_url",
      type: "upload",
      maxCount: 1,
      listType: "picture-card",
    },
    {
      label: "内容",
      name: "content",
      type: "richtext",
      rules: [{ required: true, message: "请输入内容" }],
    },
    // {
    //   label: "审核状态",
    //   name: "audit_status",
    //   type: "select",
    //   options: [
    //     { label: "待审核", value: 0 },
    //     { label: "已通过", value: 1 },
    //     { label: "已拒绝", value: 2 },
    //   ],
    //   rules: [{ required: true, message: "请选择审核状态" }],
    // },
  ];

  const editFormFieldConfig: FormItemConfigs[] = [
    {
      label: "id",
      name: "id",
      type: "hidden",
    },
    {
      label: "标题",
      name: "title",
      type: "input",
      rules: [{ required: true, message: "请输入标题" }],
    },
    {
      label: "分类",
      name: "category",
      type: "input",
    },
    {
      label: "标签",
      name: "tags",
      type: "input",
      placeholder: "多个标签用逗号分隔",
    },
    {
      label: "作者",
      name: "author",
      type: "input",
    },
    {
      label: "封面图",
      name: "cover_url",
      type: "upload",
      maxCount: 1,
      listType: "picture-card",
    },
    {
      label: "内容",
      name: "content",
      type: "richtext",
      rules: [{ required: true, message: "请输入内容" }],
    },
    // {
    //   label: "审核状态",
    //   name: "audit_status",
    //   type: "select",
    //   options: [
    //     { label: "待审核", value: 0 },
    //     { label: "已通过", value: 1 },
    //     { label: "已拒绝", value: 2 },
    //   ],
    //   rules: [{ required: true, message: "请选择审核状态" }],
    // },
  ];

  const detailFieldConfig: DetailFieldConfig[] = [
    {
      title: "序号",
      dataIndex: "_index",
    },
    {
      title: "ID",
      dataIndex: "id",
    },
    {
      title: "标题",
      dataIndex: "title",
    },
    {
      title: "分类",
      dataIndex: "category",
    },
    {
      title: "标签",
      dataIndex: "tags",
    },
    {
      title: "作者",
      dataIndex: "author",
    },
    {
      title: "封面图",
      dataIndex: "cover_url",
      span: 2,
      render: (text) => (
        <AdminImage src={text} alt="" style={{ width: "100%" }} preview />
      ),
    },
    {
      title: "内容",
      dataIndex: "content",
      span: 2,
      render: (text) => <MarkParse text={text} />,
    },
    // {
    //   title: "审核状态",
    //   dataIndex: "audit_status",
    //   render: (text: number) => {
    //     switch(text) {
    //       case 0: return "待审核";
    //       case 1: return "已通过";
    //       case 2: return "已拒绝";
    //       default: return "未知";
    //     }
    //   },
    // },
    {
      title: "状态",
      dataIndex: "is_deleted",
      render: (text: number) => text === 1 ? "已删除" : "正常",
    },
    {
      title: "创建时间",
      dataIndex: "created_at",
      render: (text: string) => dayjs(text).format("YYYY-MM-DD HH:mm:ss"),
    },
    {
      title: "更新时间",
      dataIndex: "updated_at",
      render: (text: string) => dayjs(text).format("YYYY-MM-DD HH:mm:ss"),
    },
  ];

  return (
    <Manager
      apiConfig={apiConfig}
      tableColumnConfig={tableColumnConfig}
      searchFormFieldConfig={searchFormFieldConfig}
      editFormFieldConfig={editFormFieldConfig}
      addFormFieldConfig={addFormFieldConfig}
      detailFieldConfig={detailFieldConfig}
      enableAdd
      enableEdit
      enableDelete
      defaultViewType="list"
      addModalWidth={800}
      editModalWidth={800}
      detailModalWidth={800}
    />
  );
}
